package com.yyq.gxdc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyq.gxdc.bean.Result;
import com.yyq.gxdc.entity.User;
import com.yyq.gxdc.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @version 1.0
 * @create 2020/10/30 19:21
 */
@CrossOrigin
@RestController
@RequestMapping("")
public class LoginController {

    @Autowired
    private IUserService userService;

    /**
     * 登录
     * @param username
     * @param password
     * @param req
     * @return
     */
    @GetMapping(value = "/loginUser")
    public Result<Object> loginUser(String username , String password, HttpServletRequest request) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User loginUser = userService.loginUser(user);
        if (loginUser!=null){
            request.getSession().setAttribute("user", loginUser.getId());
//            return Result.ok();
        }else{
            return Result.error("登录失败！");
        }
        return Result.ok(loginUser);
    }

    /**
     * 注册
     * @param user
     * @param req
     * @return
     */
    @GetMapping(value = "/registerUser")
    public Result<Object> registerUser(User user, HttpServletRequest req) {
        // 先查询 这个 登录名 是否已存在
        User checkUserName = userService.findUserByUserName(user.getUsername());
        if(checkUserName != null){
            return Result.error(user.getUsername()+":登录账户名已被注册！");
        }
        // 检查手机号  登录
        User checkPhone = userService.findUserByUserName(user.getPhone());
        if(checkPhone != null){
            return Result.error("手机号已被绑定！");
        }
        // 然后开始注册 保存该用户信息到数据库
        user.setUsetype("0");
        userService.save(user);
        return Result.ok();
    }

    /**
     * 查看登陆用户身份
     * @param request
     * @return
     */
    @ApiOperation("获取用户登录信息接口")
    @GetMapping("/session")
    public Result<Object> session(HttpServletRequest request){
        Object uid = request.getSession().getAttribute("user");
        if (uid!=null){
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("id",uid);
            User user = userService.getOne(wrapper);
            return Result.ok(user);
        }else{
            return Result.ok(null);
        }

    }

    /**
     * 退出
     * @param request
     * @param response
     * @throws Exception
     */
    @ApiOperation("退出接口")
    @PostMapping("/exit")
    public Result<Object> exit(HttpServletRequest request, HttpServletResponse response)throws Exception{
        HttpSession admin = request.getSession();
        admin.removeAttribute("user");
        //跳转登陆页面
        //response.sendRedirect("login.html");
        return Result.ok();
    }
}
